/**
 * @license
 * Copyright Akveo. All Rights Reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 */


/**
 * This is a starting point where we declare the maps of themes and globally available functions/mixins
 */

@import 'core/mixins';
@import 'core/functions';

$nb-enabled-themes: () !global;
$nb-enable-css-variables: false !global;

$nb-themes: () !global;
$nb-themes-non-processed: () !global;
$nb-themes-export: () !global;

@function nb-theme($key) {
  @return map-get($theme, $key);
}

@function nb-get-value($theme, $key, $value) {
  @if (type-of($value) == 'string') {
    $tmp: map-get($theme, $value);

    @if ($tmp != null) {
      @return nb-get-value($theme, $value, $tmp);
    }
  }

  @return map-get($theme, $key);
}

@function convert-to-css-variables($variables) {
  $result: ();
  @each $var, $value in $variables {
    $result: map-set($result, $var, '--var(#{$var})');
  }

  @debug $result;
  @return $result;
}

@function set-global-theme-vars($theme, $theme-name) {
  $theme: $theme !global;
  $theme-name: $theme-name !global;
  @if ($nb-enable-css-variables) {
    $theme: convert-to-css-variables($theme) !global;
  }
  @return $theme;
}

@function nb-register-theme($theme, $name, $default: null) {

  $theme-data: ();


  @if ($default != null) {

    $theme: map-merge(map-get($nb-themes-non-processed, $default), $theme);
    $nb-themes-non-processed: map-set($nb-themes-non-processed, $name, $theme) !global;

    $theme-data: map-set($theme-data, data, $theme);
    $nb-themes-export: map-set($nb-themes-export, $name, map-set($theme-data, parent, $default)) !global;

  } @else {
    $nb-themes-non-processed: map-set($nb-themes-non-processed, $name, $theme) !global;

    $theme-data: map-set($theme-data, data, $theme);
    $nb-themes-export: map-set($nb-themes-export, $name, map-set($theme-data, parent, null)) !global;
  }

  $theme-parsed: ();
  @each $key, $value in $theme {
    $theme-parsed: map-set($theme-parsed, $key, nb-get-value($theme, $key, $value));
  }

  // enable right away when installed
  $theme-parsed: set-global-theme-vars($theme-parsed, $name);
  @return map-set($nb-themes, $name, $theme-parsed);
}

@function get-enabled-themes() {
  $themes-to-install: ();

  @if (length($nb-enabled-themes) > 0) {
    @each $theme-name in $nb-enabled-themes {
      $themes-to-install: map-set($themes-to-install, $theme-name, map-get($nb-themes, $theme-name));
    }
  } @else {
    $themes-to-install: $nb-themes;
  }

  @return $themes-to-install;
}

@mixin install-css-variables($theme-name, $variables) {
  .nb-theme-#{$theme-name} {
    @each $var, $value in $variables {
      --#{$var}: $value;
    }
  }
}

// TODO: we hide :host inside of it which is not obvious
@mixin nb-install-component() {

  $themes-to-install: get-enabled-themes();

  @each $theme-name, $theme in $themes-to-install {
    /*
      :host can be prefixed
      https://github.com/angular/angular/blob/8d0ee34939f14c07876d222c25b405ed458a34d3/packages/compiler/src/shadow_css.ts#L441

      We have to use :host insted of :host-context($theme), to be able to prefix theme class
      with something defined inside of @content, by prefixing &.
      For example this scss code:
        .nb-theme-default {
          .some-selector & {
            ...
          }
        }
      Will result in next css:
        .some-selector .nb-theme-default {
          ...
        }

      It doesn't work with :host-context because angular splitting it in two selectors and removes
      prefix in one of the selectors.
    */
    .nb-theme-#{$theme-name} :host {
      $theme: set-global-theme-vars($theme, $theme-name);
      @content;
    }
  }
}

@mixin nb-for-theme($name) {
  @if ($theme-name == $name) {
    @content;
  }
}

@mixin nb-except-theme($name) {
  @if ($theme-name != $name) {
    @content;
  }
}

// TODO: another mixing for the almost same thing
@mixin nb-install-root-component() {
  @warn '`nb-install-root-component` is depricated, replace with `nb-install-component`, as `body` is root element now';

  @include nb-install-component() {
    @content;
  }
}

@mixin nb-install-global() {
  $themes-to-install: get-enabled-themes();

  @each $theme-name, $theme in $themes-to-install {
    .nb-theme-#{$theme-name} {
      $theme: set-global-theme-vars($theme, $theme-name);
      @content;
    }
  }
}
